package service

import "gitee.com/gopher2011/mall/app/dao"

// 全局公开对象
var PmsBrand = pmsBrandService{}

// 私有对象，所有的方法都绑定到这个结构体上。

type pmsBrandService struct{}

func (p *pmsBrandService) ListAll() ([]dao.PmsBrand, error) {
	return dao.PmsBrandDao.ListAll()
}

func (p *pmsBrandService) Create(arg dao.PmsBrandParam) (int64, error) {
	return dao.PmsBrandDao.InsertTemplate(arg)
}

func (p *pmsBrandService) Update(id int64, arg dao.PmsBrandParam) (int64, error) {
	arg.Id = id
	//如果创建时首字母为空，取名称的第一个为首字母
	if arg.FirstLetter == "" {
		b := []byte(arg.Name)
		arg.FirstLetter = string(b[0])
	}
	//更新品牌时要更新商品中的品牌名称
	res, err := dao.PmsProductDao.UpdateBrandName(id, arg.Name)
	if err != nil {
		return res, err
	}
	return dao.PmsBrandDao.UpdateTemplate(arg)
}

func (p *pmsBrandService) Delete(id int) (int64, error) {
	return dao.PmsBrandDao.DeleteTemplate(id)
}

func (p *pmsBrandService) DeleteBatch(ids []int) (int64, error) {
	return dao.PmsBrandDao.DeleteBatch(ids)
}

func (p *pmsBrandService) List(arg dao.PmsBrandList) ([]dao.PmsBrand, error) {
	arg.PageNum = (arg.PageNum - 1) * arg.PageSize
	return dao.PmsBrandDao.List(arg)
}

func (p *pmsBrandService) GetItem(id int) (dao.PmsBrand, error) {
	return dao.PmsBrandDao.SelectTemplate(id)
}

func (p *pmsBrandService) UpdateShowStatus(ids []int, showStatus int) (int64, error) {
	return dao.PmsBrandDao.UpdateShowStatus(ids, showStatus)
}

func (p *pmsBrandService) UpdateFactory(ids []int, factoryStatus int) (int64, error) {
	return dao.PmsBrandDao.UpdateFactoryStatus(ids, factoryStatus)
}
